#!/bin/bash

# Linux Auto-Install Git
# 
# Copyright (c) 2012 Danijel James http://danijelj.com
#
# Licensed under MIT

# version number
version="1.1.1"

# Check that running on Ubuntu or Debian
error(){
  printf "\n\033[31m$*\033[00m"
  return 0
}

# request `sudo` access
function reqSudo {
sudo -v
}

# keep-alive
function keepAlive {
while true; do sudo -n true; sleep 60; kill -0 '$$' || exit; done 2>/dev/null &
}

# Function Show script help
function showHelp {
        clear
        echo "Linux Auto-Install Git $version"
        echo "Copyright © 2012 Danijel James"
        echo 
        echo 'Usage: $0 [option]'
        echo 
        echo 'Options:'
        echo '  -h | --help             This help file'
        echo '  -i | --install          Install Git without updating packages'
        echo '  -u | --update-install   Update packages and install Git'
        echo 
        echo 
}

# apt-get upgrade full
function agUpdateFull {
sudo apt-get -y update
sudo apt-get -y dist-upgrade
sudo apt-get -y autoremove
sudo apt-get -y autoclean
}

# apt-get upgrade
function agUpdate {
sudo apt-get -y update
}

# install Git dependencies
function gitDepends {
sudo apt-get -y install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
}

# install git 
function gitInstall {
git clone git://git.kernel.org/pub/scm/git/git.git
cd git
make prefix=/usr/local all
sudo make prefix=/usr/local install
}

# call Install
function callI {
reqSudo
keepAlive
agUpdate
gitDepends
gitInstall
}

# call Update-Install
function callU {
reqSudo
keepAlive
agUpdateFull
gitDepends
gitInstall
}

# Error & Exit Notice
error(){
  printf "\n\033[31m$*\033[00m"
  return 0
}

# Check running Linux
if [ `uname -s` != "Linux" ] ; then
        error "This script is designed to be run on Ubuntu or Debian\nExiting...\n"
        exit 0
elif [ ! -f /etc/debian_release ] ; then
        error "This script is designed to be run on Ubuntu or Debian\nExiting...\n"
        exit 0
elif [ -f /etc/debian_release ] ; then
        showHelp
fi

# Main method starts here
case $1 in
        -h | --help )            showHelp
                                 exit
                                 ;;
        -i | --install )         callI
                                 exit
                                 ;;
        -u | --update-install )  callS   
                                 exit
                                 ;;
        * )                      showHelp
                                 exit 1
esac



# Novell SuSE---> /etc/SuSE-release
# Red Hat--->/etc/redhat-release, /etc/redhat_version
# Fedora-->/etc/fedora-release
# Slackware--->/etc/slackware-release, /etc/slackware-version
# Debian--->/etc/debian_release, /etc/debian_version
# Mandrake--->/etc/mandrake-release
# Yellow dog-->/etc/yellowdog-release
# Sun JDS--->/etc/sun-release
# Solaris/Sparc--->/etc/release
# Gentoo--->/etc/gentoo-release
#
# cat /etc/*-release